const mysql = require('mysql');

function query(sql, params = []) {
    const con = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: 'root',
        database: 'koa',
    });

    con.connect((error) => {
        if (error) return console.log('连接数据库失败');
    });

    return new Promise(function(resolve, reject) {
        // 执行查询
        con.query(sql, params, (error, data) => {
            if (error) return reject(error);
            resolve(data);
        });
        // 结束
        con.end();
    });
}

module.exports = {
    query,
};
